﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using Bestlife.ERP.DataAccess;

/// <summary>
/// Summary description for D_Stock
/// </summary>
/// 
namespace Bestlife.ERP.DataAccess
{
    public class D_Stock
    {
        Decimal QuantityDB = 0;
        SqlDataReader rdR_Status;
        //List<object> productset = new List<object>();
        public D_Stock()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Decimal ReturnStockQuantity(string pro_Id,string p_code)
        {
            try
            {
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                String Ssql = "SELECT Qty_in_stock from Tbl_Stock  where Pro_ID='" + pro_Id + "'AND  Code='" + p_code + "'";
                //SqlDataReader ldr = Bestlife.ERP.DataAccess.clsCon.ReadTable(Ssql);


                rdR_Status = clsCon.ReadTable(Ssql);


                if (rdR_Status.Read())
                {
                    QuantityDB = Decimal.Parse(rdR_Status["Qty_in_stock"].ToString());
                }
            }
            catch (Exception)
            {

            }
            finally
            {
                rdR_Status.Close();
            }

            return QuantityDB;
        }

        public decimal reduceStock(string Pro_id, string Pro_code, double qty)
        {
            decimal upd2 = 0;
            Decimal QuantityinDB = ReturnStockQuantity(Pro_id, Pro_code);

            if (QuantityinDB >=0)
            {
                decimal assignqty = Decimal.Parse(qty.ToString());
                if (QuantityinDB >= assignqty)
                {
                    Decimal QuantityToDB = QuantityinDB - Decimal.Parse(qty.ToString());
                    if (QuantityToDB > 0)
                    {
                        String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                        Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                        String updtxt2 = "UPDATE Tbl_Stock SET  Qty_in_stock ='" + QuantityToDB + "',Last_Modify='" + System.DateTime.Now + "' WHERE Pro_ID='" + Pro_id + "'AND Code='" + Pro_code + "'";
                        // String updtxt2 = "UPDATE Bonus  SET  T_commision='999' WHERE User_id='001'";
                        clsCon.AddEditDel(updtxt2);
                        upd2 = 0;
                    }
                    else
                    {
                        upd2 = QuantityinDB; //print we have only QuantityinDB now !!!
                    }
                }
                else
                {
                    upd2 = QuantityinDB;
                }

            }
            else
            {
                upd2 = QuantityinDB;   // sory we have nt any Product 
            }

            return upd2;
        }

        //Ryan

        //public decimal specialpack(string Pro_Name, double qty)
        //{
        //    decimal upd2 = 0;
        //    Decimal QuantityinDB = ReturnStockQuantity(Pro_Name);
           
        //    if (Pro_Name == "Herbal Pack")
        //    {

        //        productset.Add("Lion barm 30g");
        //        productset.Add("Seevana Shampoo 100ml");
        //        productset.Add("Kunkumadeelepam 100g");
        //        productset.Add("Slimin (150tablets)");
        //        productset.Add("Bright Cream 15g");
        //        productset.Add("Osethma Mouth Wash 200ml");
        //    }
           
        //    if (QuantityinDB >= 0)
        //    {
        //        decimal assignqty = Decimal.Parse(qty.ToString());
        //        if (QuantityinDB >= assignqty)
        //        {
        //            Decimal QuantityToDB = QuantityinDB - Decimal.Parse(qty.ToString());
        //            if (QuantityToDB > 0 || Pro_Name=="Herbal Pack")
        //            {
        //                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
        //                Bestlife.ERP.DataAccess.clsCon.connect(connStr);
        //                String updtxt2 = "UPDATE Tbl_Stock SET  Qty_in_stock ='" + QuantityToDB + "',Last_Modify='" + System.DateTime.Now + "' WHERE Pro_Name='" + productset + "'";
        //                // String updtxt2 = "UPDATE Bonus  SET  T_commision='999' WHERE User_id='001'";
        //                clsCon.AddEditDel(updtxt2);
        //                upd2 = 0;
        //            }
        //            else
        //            {
        //                upd2 = QuantityinDB; //print we have only QuantityinDB now !!!
        //            }
        //        }
        //        else
        //        {
        //            upd2 = QuantityinDB;
        //        }

        //    }
        //    else
        //    {
        //        upd2 = QuantityinDB;   // sory we have nt any Product 
        //    }

        //    return upd2;
        //}


    }
}